package com.yimicloud.common.svn;


import com.yimicloud.common.util.JsonUtil;

import java.io.*;

/**
 * Created by Justin on 2015/9/7.
 */
public class PaaswordUtil {
    public static void main(String[] args) {
        PaaswordUtil untils = new PaaswordUtil();

        try {
            String e = "E:/svndemo/conf/passwd";
            SvnUser usersItems = new SvnUser();
            usersItems.setUserName("caozhi");
            usersItems.setPassword("111111");
            String old = "caozhi";
            String content = untils.updatePasswd(usersItems, e);
            untils.writePasswd(e, content);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

    }

    public String addPasswd(SvnUser items, String path) throws IOException {
        System.out.println("svnpasswd路径为 ===========》" + path);
        JsonUtil jsonUtil = new JsonUtil();
        File file = new File(path);
        if(!file.exists()) {
            jsonUtil.jsonPut("status", "error");
            jsonUtil.jsonPut("message", "passwd文件没有找到，请检查您的svnpasswd地址配置是否正确");
            return jsonUtil.getJsonText();
        } else {
            System.out.println("文件已经存在");
            new String();
            String s1 = new String();

            String s;
            BufferedReader input;
            for(input = new BufferedReader(new FileReader(file)); (s = input.readLine()) != null; s1 = s1 + s + "\n") {
                ;
            }

            input.close();
            s1 = s1 + items.getUserName() + " = " + items.getPassword() + "\n";
            BufferedWriter output = new BufferedWriter(new FileWriter(file));
            output.write(s1);
            output.close();
            jsonUtil.jsonPut("status", "success");
            jsonUtil.jsonPut("message", "svn密码添加成功");
            return jsonUtil.getJsonText();
        }
    }

    public String updatePasswd(SvnUser items, String path) throws IOException {
        System.out.println("svnpasswd路径为 ===========》" + path);
        BufferedReader br = null;
        String line = null;
        StringBuffer buf = new StringBuffer();

        try {
            br = new BufferedReader(new FileReader(path));

            while((line = br.readLine()) != null) {
                System.out.println(line);
                if(line.startsWith(items.getUserName())) {
                    String e = items.getUserName() + " = " + items.getPassword() + "\n";
                    buf.append(e);
                    buf.append(System.getProperty("line.separator"));
                } else {
                    buf.append(line);
                    buf.append(System.getProperty("line.separator"));
                }
            }
        } catch (Exception var15) {
            var15.printStackTrace();
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException var14) {
                    br = null;
                }
            }

        }

        return buf.toString();
    }

    public String deletePasswd(String userName, String path) {
        BufferedReader br = null;
        String line = null;
        StringBuffer buf = new StringBuffer();

        try {
            br = new BufferedReader(new FileReader(path));

            while((line = br.readLine()) != null) {
                if(!line.startsWith(userName)) {
                    buf.append(line);
                    buf.append(System.getProperty("line.separator"));
                }
            }
        } catch (Exception var15) {
            var15.printStackTrace();
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException var14) {
                    br = null;
                }
            }

        }

        return buf.toString();
    }

    public void writePasswd(String path, String content) {
        BufferedWriter bw = null;

        try {
            bw = new BufferedWriter(new FileWriter(path));
            bw.write(content);
        } catch (Exception var13) {
            var13.printStackTrace();
        } finally {
            if(bw != null) {
                try {
                    bw.close();
                } catch (IOException var12) {
                    bw = null;
                }
            }

        }

    }
}
